<template>
  <div class="notices-card">
    <div class="content">
      <div class="title">
        <h3 @click="gotoNotices">
          {{ notice?.title }}
          <span style="color: red"> (最新)</span>
        </h3>
      </div>
      <div class="info">
        <span>
          <el-icon class="icon"><Avatar /></el-icon>发布者：{{ notice?.publisher }}
        </span>
        <span>
          <el-icon class="icon"><User /></el-icon>接收者：{{ subscriber(notice?.subscriber) }}
        </span>
        <span>
          <el-icon class="icon"><Timer /></el-icon>发布日期：{{ notice?.pub_date }}
        </span>
      </div>
    </div>
    <footer class="footer"><span @click="gotoNotices">公告列表</span></footer>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useStore } from 'vuex';
import { ref, computed, onMounted } from 'vue';
import { getNewNotices } from '@/services/notices.js'

const router = useRouter()
const store = useStore()

const notice = ref(null)
const userInfo = computed(() => store.state.userModule.userInfo)
// 转换接收对象
const subscriber = (num) => {
  return num === '0' ? '管理员' : num === '1' ? '教师' : num === '2' ? '学生' : '所有人'
}

// 前往公告列表
const gotoNotices = () => {
  router.push(`/home/message/notices?newId=${notice.value._id}`)
}

const getNotices = async () => {
  const { data: res } = await getNewNotices(userInfo.value.role)
  notice.value = res.data.data
}

onMounted(() => {
  getNotices()
})


</script>

<style lang="less" scoped>
.notices-card {
  width: 100%;
  // height: 227px;
  height: 199px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .content {
    .title {
      padding-right: 28px;
      font-size: 22px;
      h3 {
        font-weight: normal;
        cursor: pointer;
      }
      h3:hover {
        color: #246fba;
        // color: red;
        // text-decoration: underline;
      }
    }
    .info {
      font-size: 14px;
      color: #828a92;
      span {
        margin-right: 30px;
        .icon {
          vertical-align: bottom;
          margin-right: 5px;
        }
      }
    }
  }
  .footer {
    width: 100%;
    height: 30px;
    border-top: 1px solid #eee;
    text-align: end;
    span {
      padding: 2px 5px;
      border-radius: 5px;
      color: #409eff;
      line-height: 32px;
      font-size: 14px;
      cursor: pointer;
    }
    span:hover {
      background-color: rgb(246, 245, 245);
    }
  }
}
</style>
